package org.ishequ.db;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.ishequ.domain.PhoneCall;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class PhoneDBService {

private DBOpenHelper openHelper ;
	
	public PhoneDBService(Context context){
		openHelper = new DBOpenHelper(context);
	}
	
	public long insert(PhoneCall call){
		SQLiteDatabase db = openHelper.getWritableDatabase();
		return db.insert("phone", null, buildValues(call));
	}
	
	public void update(PhoneCall call){
		SQLiteDatabase db = openHelper.getWritableDatabase();
		db.update("phone", buildValues(call), "id=?", new String[]{String.valueOf(call.getId())});
	}
	
	public void delete(int id){
		SQLiteDatabase db = openHelper.getWritableDatabase();
		db.delete("phone", "id=?", new String[]{String.valueOf(id)});
	}
	
	public List<PhoneCall> query(String selection,String[] args) throws ParseException{
		List<PhoneCall> calls = new ArrayList<PhoneCall>();
		SQLiteDatabase db = openHelper.getReadableDatabase();
		Cursor cursor = db.query("phone", new String[]{"id","sender","receiver",
							"receiverTime","duration","audioName","uploadTag",
							"length","uploadPos"},
					selection, args, "", "", "receiverTime desc");
		while(cursor.moveToNext()){
			
			String receiver = cursor.getString(cursor.getColumnIndex("receiver"));
			String sender = cursor.getString(cursor.getColumnIndex("sender"));
			Date receiveTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(cursor.getString(cursor.getColumnIndex("receiverTime")));
			String audioName = cursor.getString(cursor.getColumnIndex("audioName"));
			long duration = cursor.getLong(cursor.getColumnIndex("duration"));
			long id = cursor.getInt(cursor.getColumnIndex("id"));
			long uploadPos = cursor.getLong(cursor.getColumnIndex("uploadPos"));
			long length = cursor.getLong(cursor.getColumnIndex("length"));
			boolean uploadTag = cursor.getInt(cursor.getColumnIndex("uploadTag")) > 0;
			
			PhoneCall call = new PhoneCall();
			call.setAudioName(audioName);
			call.setDuration(duration);
			call.setId(id);
			call.setLength(length);
			call.setReceiver(receiver);
			call.setReceiveTime(receiveTime);
			call.setSender(sender);
			call.setUploadPos(uploadPos);
			call.setUploadTag(uploadTag);
			
			calls.add(call);
			System.out.println(call);
		}
		cursor.close();
		return calls;
	}
	
	private ContentValues buildValues(PhoneCall call) {
		ContentValues values = new ContentValues();
		values.put("sender", call.getSender());
		values.put("receiver", call.getReceiver());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		values.put("receiverTime", sdf.format(call.getReceiveTime()));
		values.put("duration", String.valueOf(call.getDuration()));
		values.put("audioName", call.getAudioName());
		values.put("uploadPos", call.getUploadPos());
		return values;
	}
	
	public void close(){
		openHelper.close();
	}

}
